package com.zsh.web.controller;


import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.zsh.web.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author: 003089
 * @Description:
 * @Date: 2019/8/6 15:16
 */
@RestController
@RequestMapping("/web")
public class WebController {

    @Autowired
    private HelloService helloService;

   /* @NacosValue(value ="${nacos.test:123}", autoRefreshed = true )
    private String userAge;*/

   /* @NacosValue(value ="${userName:null}", autoRefreshed = true )
    private String userName;

        static {
            List<FlowRule> rules = new ArrayList<>();
            FlowRule rule = new FlowRule();
            rule.setResource("HelloWorld");
            rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
            // Set limit QPS to 20.
            rule.setCount(2);
            rules.add(rule);
            FlowRuleManager.loadRules(rules);
        }*/

    @RequestMapping(value = "/hello")

    public String hello() {
        try {
            return helloService.helloService();
        }catch (Exception e){
            return "error";
        }

    }

    // blockHandler 函数，原方法调用被限流/降级/系统保护的时候调用
    public String blockHandlerForGetUser( BlockException ex) {
        return "block";
    }


   /* // 原函数
    @SentinelResource(value = "hello", blockHandler = "exceptionHandler", fallback = "helloFallback")
    public String hello(long s) {
        return String.format("Hello at %d", s);
    }*/


}
